﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.ComponentModel;

namespace Legend.Core.Tests.TypeConverterBaseTests
{
    [TestFixture]
    public class CanConvertTo
    {
        [TestAttribute]
        public void should_return_true_when_type_matches_destination_type()
        {
            var c = new DummyIntegerConverter();
            Assert.IsTrue(c.CanConvertTo(typeof(string)));
        }

        [TestAttribute]
        public void should_return_false_when_type_does_not_match_destination_type_of_converter()
        {
            var c = new DummyIntegerConverter();
            Assert.IsFalse(c.CanConvertTo(typeof(int)));
        }

    }

    [TestFixture]
    public class CanConvertFrom
    {
        [TestAttribute]
        public void should_return_true_when_type_matches_destination_type_of_converter()
        {
            var c = new DummyIntegerConverter();
            Assert.IsTrue(c.CanConvertFrom(typeof(string)));
        }

        [TestAttribute]
        public void should_return_false_when_type_does_not_match_destination_type_of_converter()
        {
            var c = new DummyIntegerConverter();
            Assert.IsFalse(c.CanConvertFrom(typeof(int)));
        }
    }

    [TestFixture]
    public class ConvertTo
    {
        [TestAttribute]
        public void should_convert_the_value_to_destination_type()
        {
            var c = new DummyIntegerConverter();
            string converted = c.ConvertTo(10);
            Assert.AreEqual("10", converted);
        }

        [Test, ExpectedException(typeof(NotSupportedException))]
        public virtual void should_throw_when_destination_type_is_not_same_as_the_generic_destination_type()
        {
            var c = new DummyIntegerConverter();
            c.ConvertTo(10, typeof (bool));
        }
    }

    [TestFixture]
    public class ConvertFrom
    {
        [TestAttribute]
        public void should_convert_the_value_to_the_source_type_of_the_converter()
        {
            var c = new DummyIntegerConverter();
            Assert.AreEqual(10, c.ConvertFrom("10"));
        }

        [Test, ExpectedException(typeof(NotSupportedException))]
        public virtual void should_throw_when_value_is_not_of_destination_type()
        {
            var c = new DummyIntegerConverter();
            c.ConvertFrom(10);
        }
    }



    public class DummyIntegerConverter
        : TypeConverterBase<int, string>
    {

        protected override int ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, string value)
        {
            return int.Parse(value, culture);
        }

        protected override string ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, int value)
        {
            return value.ToString(culture);
        }
    }
}